package com.goodfox.web.generic.area.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.area.controller.AreaController;
import com.goodfox.web.generic.area.entity.Area;
import com.goodfox.web.generic.area.mapper.AreaMapper;
import com.goodfox.web.generic.area.service.AreaService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;

/**
 * 地区Service实现类
 * @author qin gt
 *
 */
@Transactional
@Service(value="areaService")
@NoteExplain(comment="地区管理")
public class AreaServiceImpl implements AreaService{

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(AreaController.class);

	@Autowired
	private AreaMapper areaMapper;

	@NoteExplain(comment="查询地区列表")
	public List<Area> queryAreaListByParentId(String areaIdParent){
		Map map=new HashMap();
		map.put("areaIdParent", areaIdParent);
		return areaMapper.selectAreaList(map);
	}
	
	@NoteExplain(comment="获取地区")
	public Area getArea(String areaId){
		return areaMapper.selectAreaById(areaId);
	}
	
	@NoteExplain(comment="新增地区",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addArea(Area entity,String loginLogId){
		if(entity.getAreaGradeParent()!=null && !entity.getAreaGradeParent().equals("")){
			entity.setAreaGrade(entity.getAreaGradeParent()+1);
		}else{
			entity.setAreaGrade(1L);
		}
		if(entity.getAreaSort()==null){
			Map map=new HashMap();
			map.put("areaIdParent", entity.getAreaIdParent());
			entity.setAreaSort(areaMapper.selectSubAreaListCount(map)+1);
		}
		areaMapper.insertArea(entity);
	}

	@NoteExplain(comment="更新地区",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyArea(Area oldEny,Area newEny,String loginLogId){
		if(newEny.getAreaGradeParent()!=null && !newEny.getAreaGradeParent().equals("")){
			newEny.setAreaGrade(newEny.getAreaGradeParent()+1);
		}else{
			newEny.setAreaGrade(1L);
		}
		if(newEny.getAreaSort()==null){
			Map map=new HashMap();
			map.put("areaIdParent", newEny.getAreaIdParent());
			newEny.setAreaSort(areaMapper.selectSubAreaListCount(map)+1);
		}
		areaMapper.updateArea(newEny);
	}

	@NoteExplain(comment="移除地区",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeArea(Area entity,String loginLogId){
		areaMapper.deleteAreaById(entity.getAreaId());
	}
	
	@NoteExplain(comment="查询子地区列表")
	public Map querySubAreaList(Map map){
		Map subAreaMap=new HashMap();
		for(Area area:areaMapper.selectSubAreaList(map)){
			subAreaMap.put(area.getAreaId(),area.getAreaName());
		}
		return subAreaMap;
	}

	@NoteExplain(comment="查询地区集合")
	public Set<Area> queryAreaSetByMultiId(String areaId){
		Map map=new HashMap();
		if(areaId!=null && !areaId.equals("")){
			map.put("areaId", areaId);
		}
		return orderFillingAreaSet(areaMapper.selectAreaListByMultiId(map));
	}
	
	@NoteExplain(comment="填充地区集合")
	public Set<Area> orderFillingAreaSet(List<Area> areaList){
		TreeSet<Area> topAreaSet=new TreeSet<Area>();
		Set<Area> childAreaSet=new HashSet<Area>();
		for(Area oriArea:areaList){
			if(oriArea.getAreaGrade().equals(1L)){
				topAreaSet.add(oriArea);
			}else{
				childAreaSet.add(oriArea);
			}
		}
		for(Area topArea:topAreaSet){
			areaAddChild(topArea,childAreaSet,0);
		}
		return topAreaSet;
	}
	
	@NoteExplain(comment="功能添加子地区")
	private void areaAddChild(Area topArea,Set<Area> oriAreaSet,int count){
		TreeSet<Area> areaSet=new TreeSet<Area>();
		for(Area oriArea:oriAreaSet){
			if(null!=oriArea.getAreaIdParent()){
				if(topArea.getAreaId().equals(oriArea.getAreaIdParent())){
					areaSet.add(oriArea);
					count++;
				}
			}
		}
		if(areaSet.size()>0){
			topArea.setRealSubAreaSet(areaSet);
			for(Area area:topArea.getRealSubAreaSet()){
				areaAddChild(area,oriAreaSet,count);
			}
			topArea.setRealSubAreaCount((long)topArea.getRealSubAreaSet().size());
		}
		if(count==oriAreaSet.size()){
			return;
		}
	}
}
